<?php

namespace App\Http\Controllers\Api;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MenuController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\Response|void
     * @throws ValidationException
     */
    public function index(Request $request)
    {
        $input = $request->input();
        $language = $this->_language($input);
    	$uids_object = Db::table('menu_permission')->where('uid','=',$request->user()->id)->first();

        $where[] = ['is_display','=','1'];
    	$menu = Db::table('menu')
            ->where($where)
            ->whereIn('id',json_decode($uids_object->menu_id,1))
            ->orderBy('orders','desc')
            ->select(['id', 'orders', 'api_name', "$language as name"])
            ->get();

    	return response()->json(['code'=>'1','data'=>$menu]);
    }

    /**
     * 点击导航操作
     */
    public function category(Request $request,$category_id){
        $menu_lists = Db::table('menu_article')->where('menu_id','=',$category_id)->select(['id','title'])->get();

        return response()->json(['code'=>'1','data'=>$menu_lists]);
    }

    private function _language($languageKey)
    {
        $languageKey = isset($languageKey['language']) ? $languageKey['language'] : 0;
        $languageArr = ['ch_', 'tc_', 'en_'];
        if ($languageKey > count($languageArr)) {
            return 'ch_name';
        }
        return $languageArr[$languageKey].'name';
    }
}
